package com.service.impl;

import com.dao.TicketDao;
import com.dao.impl.TicketDaoImpl;
import com.pojo.Ticket;
import com.service.TicketService;
import com.util.JNDIUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * 机票业务实现类 调用dao层执行对应的sql
 */
public class TicketServiceImpl implements TicketService {
    TicketDao ticketDao = new TicketDaoImpl();

    // 查询所有订单
    @Override
    public List<Ticket> selectAllTicket(String page, String limit) {
        // 通过ticketDao查询所有订单，并返回分页结果
        return ticketDao.selectAllTicket(page, limit);
    }

    /**
     * 条件查询机票信息
     *
     * @param username   用户名
     * @param startAdd   出发地
     * @param createTime 创建时间
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<Ticket> companySelectTicket(String username, String startAdd, String createTime, String page, String limit) {

        // 通过orderDao按照旅客、起始地点和创建时间查询订单，并返回分页结果
        return ticketDao.companySelectTicket(username, startAdd, createTime, page, limit);
    }

    /**
     * 生成机票
     *
     * @param ticket 订单id
     */
    @Override
    public void insertTicket(Ticket ticket) {

        ticketDao.insertTicket(ticket);

    }

    /**
     * 删除机票
     * @param ticket
     */
    @Override
    public void deleteTicket(Ticket ticket) {
        Connection conn = null;
        PreparedStatement ps = null;
        try{
            conn = JNDIUtils.getConnection();
            String sql = "DELETE FROM ticket WHERE flight_id = ? and user_id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,ticket.getFlightId());
            ps.setInt(2,ticket.getUserId());
            ps.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JNDIUtils.close(conn,ps,null);
        }
    }

    @Override
    public int selectAllTicketCount() {
        return ticketDao.selectAllTicketCount();

    }

    @Override
    public int selectTicketCountByCondition(String username, String startAdd, String createTime) {
        return ticketDao.selectTicketCountByCondition(username, startAdd, createTime);
    }


}
